package com.lanyam.travel.base.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanyam.travel.base.dao.HomeProductMapper;
import com.lanyam.travel.base.entity.HomeProduct;
import com.lanyam.travel.base.entity.Product;
import com.lanyam.travel.base.service.HomeProductService;
import com.lanyam.travel.base.service.IScenicHotelService;
import com.lanyam.travel.base.service.ITravelLineService;

@Service
public class HomeProductServiceImpl implements HomeProductService {

	@Autowired
	private HomeProductMapper homeProductMapper;
	
	@Autowired
	private IScenicHotelService scenicHotelService;
	
	@Autowired
	private ITravelLineService travelLineService;
	
	@Override
	public void addHomeProduct(HomeProduct homeProduct) {
		// TODO Auto-generated method stub
		homeProductMapper.insert(homeProduct);
	}

	@Override
	public void deleteById(String productId) {
		// TODO Auto-generated method stub
		homeProductMapper.deleteById(productId);
	}

	@Override
	public void updateHomeProduct(HomeProduct homeProduct) {
		// TODO Auto-generated method stub
		homeProductMapper.updateById(homeProduct);
	}

	@Override
	public List<HomeProduct> queryHomeProductOnShow() {
		// TODO Auto-generated method stub
//		QueryWrapper<HomeProduct> wrapper = new QueryWrapper<HomeProduct>();
//		wrapper.eq("STATUS", 1);
		List<HomeProduct> selectList = homeProductMapper.selectList(null);
		Product product;
		for (HomeProduct homeProduct : selectList) {
			if(homeProduct.getProductType() == 0)
			{
				product = scenicHotelService.getScenicById(homeProduct.getProductId());
			}
			else
			{
				product = travelLineService.getTravelLineById(homeProduct.getProductId());
			}
			homeProduct.setProduct(product);
		}
		return selectList;
	}

	@Override
	public void updateHomeProductType(String id, int status) {
		// TODO Auto-generated method stub
		homeProductMapper.updateHomeProductType(id,status);
	}

	@Override
	public List<HomeProduct> getAllHomeProduct() {
		// TODO Auto-generated method stub
		List<HomeProduct> allHomeProduct = homeProductMapper.selectList(null);
		Product product;
		for (HomeProduct homeProduct : allHomeProduct) {
			
			product = scenicHotelService.getScenicById(homeProduct.getProductId());
			
			if(null == product)
			{
				product = travelLineService.getTravelLineById(homeProduct.getProductId());
			}
			
			homeProduct.setProduct(product);
		}
		return allHomeProduct;
	}

	@Override
	public HomeProduct getHomeProductById(String bannerId) {
		// TODO Auto-generated method stub
		HomeProduct homeProduct = homeProductMapper.selectById(bannerId);
		return homeProduct;
	}

	@Override
	public List<HomeProduct> searchTitle(String key) {
		// TODO Auto-generated method stub
		List<HomeProduct> allHomeProduct = new ArrayList<HomeProduct>();
		if(StringUtils.isEmpty(key))
		{
			allHomeProduct = homeProductMapper.selectList(null);
		}
		else
		{
			QueryWrapper<HomeProduct> wrapper = new QueryWrapper<HomeProduct>();
			wrapper.like("title", key);
			allHomeProduct = homeProductMapper.selectList(wrapper);
		}
		Product product;
		for (HomeProduct homeProduct : allHomeProduct) {
			
			product = scenicHotelService.getScenicById(homeProduct.getProductId());
			
			if(null == product)
			{
				product = travelLineService.getTravelLineById(homeProduct.getProductId());
			}
			
			homeProduct.setProduct(product);
		}
		return allHomeProduct;
	}

}
